package com.tubemostwanted.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tubemostwanted.entity.Category;
import com.tubemostwanted.entity.CategoryFeed;
import com.tubemostwanted.model.CategoryModel;
import com.tubemostwanted.model.TagModel;
import com.tubemostwanted.model.TubeModel;

public class HomeController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		List<Category> listCate = CategoryModel.getTop4();
		if (listCate != null && listCate.size() > 0) {
			int count = 1;
			CategoryFeed cateFeed;
			for (Category category : listCate) {
				cateFeed = new CategoryFeed();
				cateFeed.setAlias(category.getAlias());
				cateFeed.setTitle(category.getTitle().getValue());
				cateFeed.setListTube(TubeModel.getByCategory(
						category.getAlias(), 1, 6));
				cateFeed.setCurrentPage(0);				
				request.setAttribute("result" + count, cateFeed);
				count++;
			}
			request.setAttribute("top10", TubeModel.getTop10View());
			request.setAttribute("tags", TagModel.getTop10View());
			request.setAttribute("categories", CategoryModel.all());
			request.getRequestDispatcher("home.jsp").forward(request, response);
		} else {
			response.getWriter().print("Error");
		}
	}
}
